﻿using GalaSoft.MvvmLight;
using System.Collections.Generic;
using System;
using ASoft.Repository;
using System.Collections.ObjectModel;

namespace ASoft.WPF.ViewModel
{
    /// <summary>
    /// This class contains properties that a View can data bind to.
    /// <para>
    /// Use the <strong>mvvminpc</strong> snippet to add bindable properties to this ViewModel.
    /// </para>
    /// <para>
    /// You can also use Blend to data bind with the tool's support.
    /// </para>
    /// <para>
    /// See http://www.galasoft.ch/mvvm/getstarted
    /// </para>
    /// </summary>
    public class DbLogViewModel : ViewModelBase
    {
        /// <summary>
        /// Initializes a new instance of the DbLogViewModel class.
        /// </summary>
        public DbLogViewModel()
        {
            ////if (IsInDesignMode)
            ////{
            ////    // Code runs in Blend --> create design time data.
            ////}
            ////else
            ////{
            ////    // Code runs "for real": Connect to service, etc...
            ////}
        }

        #region "RecordType"

        /// <summary>
        /// The <see cref="RecordType" /> property's name.
        /// </summary>
        public const string RecordTypePropertyName = "RecordType";

        private ERecordType _RecordType = ERecordType.RoomSearch;

        /// <summary>
        /// Gets the RecordType property.
        /// </summary>
        public ERecordType RecordType
        {
            get
            {
                return _RecordType;
            }

            set
            {
                if (_RecordType == value)
                {
                    return;
                }

                var oldValue = _RecordType;
                _RecordType = value;
                LoadDbData();
                RaisePropertyChanged(RecordTypePropertyName);
            }
        }

        #endregion

        Dictionary<ERecordType, string> _RecordTypes;
        public Dictionary<ERecordType, string> RecordTypes
        {
            get
            {
                if (_RecordTypes == null)
                {
                    _RecordTypes = AppUtility.GetEnumSources<ERecordType>();
                }
                return _RecordTypes;
            }
        }

        #region "RecordTime"

        /// <summary>
        /// The <see cref="RecordTime" /> property's name.
        /// </summary>
        public const string RecordTimePropertyName = "RecordTime";

        private System.DateTime _RecordTime = DateTime.Now;

        /// <summary>
        /// Gets the RecordTime property.
        /// </summary>
        public System.DateTime RecordTime
        {
            get
            {
                return _RecordTime;
            }

            set
            {
                if (_RecordTime == value)
                {
                    return;
                }

                var oldValue = _RecordTime;
                _RecordTime = value;

                RaisePropertyChanged(RecordTimePropertyName);
            }
        }

        #endregion



        ObservableCollection<ASoftDataEntity> _Entities = new ObservableCollection<ASoftDataEntity>();
        public ObservableCollection<ASoftDataEntity> Entities
        {
            get
            {
                if (IsInDesignMode)
                {
                    return _Entities;
                }
                LoadDbData();
                return _Entities;
            }
        }

        void LoadDbData()
        {
            var condition = new Dictionary<string, string>();
            condition["RecordTypeId"] = ((int)RecordType).ToString();
            condition["RecordTime"] = string.Format("{0}:{1}", RecordTime.ToString("yyyy-MM-dd"), RecordTime.AddDays(1).ToString("yyyy-MM-dd"));
            var records = new ASoftDataRepository().GetEntities(condition);
            _Entities.Clear();
            foreach (var record in records)
            {
                _Entities.Add(record);
            }
        }
    }
}